/*
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.ui.model;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.PerforceUtil;
import net.sourceforge.p4eclipse.core.resources.RemoteFolder;
import net.sourceforge.p4eclipse.core.syncinfo.FolderSyncInfo;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.team.core.TeamException;
import org.eclipse.ui.model.IWorkbenchAdapter;


/**
  * @version 	1.0
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class AllRootsElement implements IWorkbenchAdapter, IAdaptable
{

	/*
	 * @see IWorkbenchAdapter#getChildren(Object)
	 */
	public Object[] getChildren(Object o)
	{
		List result = new ArrayList();

		try
		{
			String[] depots = PerforceUtil.depots();
			
			for (int i = 0, length = depots.length; i < length; i++)
			{
				FolderSyncInfo[] folderSyncInfos = 
					PerforceUIPlugin.getClient().dirs(depots[i]);
				
				if (folderSyncInfos.length == 1)
					result.add(new RemoteFolder(null, folderSyncInfos[0], "#head"));
			}
		}
		catch (TeamException e)
		{
			PerforceUIPlugin.logError("AllRootsElement.getChildren failed.", e); //$NON-NLS-1$
		}
			
		return result.toArray();
	}

	/*
	 * @see IWorkbenchAdapter#getImageDescriptor(Object)
	 */
	public ImageDescriptor getImageDescriptor(Object object)
	{
		return null;
	}

	/*
	 * @see IWorkbenchAdapter#getLabel(Object)
	 */
	public String getLabel(Object o)
	{
		return null;
	}

	/*
	 * @see IWorkbenchAdapter#getParent(Object)
	 */
	public Object getParent(Object o)
	{
		return null;
	}

	/*
	 * @see IAdaptable#getAdapter(Class)
	 */
	public Object getAdapter(Class adapter)
	{
		return (adapter == IWorkbenchAdapter.class) ? this : null;
	}

}
